package com.frank.redis.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.frank.jpaBatchSave.entity.User;
import com.frank.redis.domain.Student;
import com.frank.redis.service.TestService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 小石潭记
 * @date 2020/10/19 11:26
 * @Description: ${todo}
 */


@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    protected StringRedisTemplate redisTemplate;

    private static final String REDIS_STUDENT_KEY = "redis:hash-key:student-map:";

    private static final String REDIS_USER_KEY = "redis:hash-key:user-map:";

    @Autowired
    private TestService testService;

    @GetMapping("/getAllUser")
    public List<User> getAllUser() {
        return testService.getAllUser();
    }

    @GetMapping("/save")
    public void testRedis() {
        List<Student> studentList = new ArrayList<>();
        Student s1 = new Student(1, "张三", 1);
        Student s2 = new Student(2, "李四", 1);
        Student s3 = new Student(3, "王五", 1);
        studentList.add(s1);
        studentList.add(s2);
        studentList.add(s3);

        //list转Map,redis中的hash结构key-value为String-String
        Map<String, String> map = studentList.stream()
                .collect(Collectors.toMap(student -> student.getId() + "", student -> JSON.toJSONString(student)));

        redisTemplate.opsForHash().putAll(REDIS_STUDENT_KEY, map);

        // 例子2
        Map<String, String> nameAgeMap = new HashMap<>();
        nameAgeMap.put("name", "alice");
        nameAgeMap.put("address", "beijing");
        nameAgeMap.put("age", "18");
        redisTemplate.opsForHash().putAll(REDIS_USER_KEY, nameAgeMap);

    }

    @GetMapping("/info")
    public void getUserInfo() {
        List<Object> studentListObj = redisTemplate.opsForHash().values(REDIS_STUDENT_KEY);
        if (studentListObj == null) {
            return;
        }
        Object objTemp = studentListObj;
        List<Student> studentList = JSONArray.parseArray(objTemp.toString(), Student.class);
        System.out.println(studentList);

        // 例子2
        Map<Object, Object> nameAgeMap = redisTemplate.opsForHash().entries(REDIS_USER_KEY);
        nameAgeMap.forEach((k, v) -> System.out.println("key===" + k + " value===" + v));
        Object o = redisTemplate.opsForHash().get(REDIS_USER_KEY, "name");
        System.out.println("name是：" + o);

    }

    @DeleteMapping("/{id}")
    public void deleteKey(@PathVariable("id") int id) {
        // 删除指定的key  ??? 失败
        redisTemplate.opsForHash().delete(REDIS_STUDENT_KEY + "_" + id, String.valueOf(id));
    }

    @GetMapping("/keys")
    public Object getKeys() {
        Set<Object> hashKeys = redisTemplate.opsForHash().keys(REDIS_STUDENT_KEY);
        return hashKeys;
    }


}
